package com.workflow.itrun.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.workflow.itrun.dto.RequestDTO;

import com.workflow.itrun.entity.ItRequest;
import com.workflow.itrun.entity.ItRequestExample;
import com.workflow.itrun.mapper.ItRequestMapper;
import com.workflow.itrun.service.RequestService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service
public class RequestServiceImpl implements RequestService {

    @Autowired
    private ItRequestMapper itRequestMapper;

    public PageInfo<ItRequest> selectRequestList(Integer pageIndex, Integer pageSize){
        PageHelper.startPage(pageIndex,pageSize);
        ItRequestExample itRequestExample = new ItRequestExample();
        List<ItRequest> requests = itRequestMapper.selectByExample(itRequestExample);
        PageInfo<ItRequest> requestInfo = new PageInfo<ItRequest>(requests);
        return requestInfo;
    }

    @Override
    public ItRequest saveRequest(RequestDTO requestDTO) {
        ItRequest request = new ItRequest();
//        request.setReqCategory(requstDTO.getReqCategory());
//        request.setReqDesc(requstDTO.getReqDesc());
//        request.set
        BeanUtils.copyProperties(requestDTO,request);
        itRequestMapper.insertSelective(request);
        return request;
    }

    @Override
    public ItRequest mergeRequest(RequestDTO requestDTO) {
        ItRequest request = new ItRequest();
        BeanUtils.copyProperties(requestDTO,request);
        ItRequestExample requestExample = new ItRequestExample();
        ItRequestExample.Criteria criteria = requestExample.createCriteria();
        criteria.andIdEqualTo(request.getId());
        itRequestMapper.updateByExampleSelective(request,requestExample);
        return request;
    }

    @Override
    public void deleteRequest(List<Integer> ids) {
        ItRequestExample requestExample = new ItRequestExample();

        ItRequestExample.Criteria criteria = requestExample.createCriteria();
        criteria.andIdIn(ids);

        itRequestMapper.deleteByExample(requestExample);
    }


}
